package com.zwl.springcloud.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zwl.springcloud.utlis.JSONUtils;
import com.zwl.springcloud.utlis.TokenUtil;
import com.zwl.springcloud.config.RedisUtil;
import com.zwl.springcloud.dao.UserMapper;
import com.zwl.springcloud.entity.Result;
import com.zwl.springcloud.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/api")
public class RequestControl {

    @Autowired // 自动注入
    private UserMapper userDao;
    @Autowired RedisUtil redisUtil;

    @PostMapping("/login")
    public Result<User> login(@RequestBody JSONObject jsonParam) {
        User user = JSONUtils.fromJson(jsonParam.toJSONString(), User.class);
        String username1 = user.getUsername();
        String password1 = user.getPassword();
        try {
            User dbUser =
                    userDao.selectOne(
                            Wrappers.<User>lambdaQuery()
                                    .eq(User::getUsername, user.getUsername()));
            if (null != dbUser) {
                if (!dbUser.getPassword().equals(password1)) {
                    return new Result<>("登陆失败,账号或密码错误", 405, user);
                } else {
                    Long currentTimeMillis = System.currentTimeMillis();
                    String token = TokenUtil.sign(username1, currentTimeMillis);
                    if (token != null) {
                        dbUser.setToken(token);
                        //                    一个月过期
                        redisUtil.set("user:session:" + username1, token, 43776 * 60 * 60);
                        return new Result<>("登陆成功", 200, dbUser);
                    }
                }
            } else {
                return new Result<>("账号不存在", 200, null);
            }

        } catch (Exception e) {
            String message = e.getMessage();
            if (null == message) {
                message = "登录失败，未知异常";
            }
            return new Result<>(message, 406, null);
        }
        return new Result<>("登陆失败", 502, user);
    }

    @ResponseBody
    @RestController // 返回jaon格式数据
    public class LoginController {
        @ResponseBody
        // 请求映射
        @PostMapping("/register")
        public Result<String> loginVefify(@RequestBody JSONObject jsonParam) {
            // 直接将json信息打印出来
            System.out.println(jsonParam.toJSONString());
            User user = JSONUtils.fromJson(jsonParam.toJSONString(), User.class);
            User userregister1 =
                    userDao.selectOne(
                            Wrappers.<User>lambdaQuery()
                                    .eq(User::getUsername, user.getUsername()));
            if (userregister1 != null) {
                return new Result<>("账号已存在", 200, null);
            }

            userDao.insert(user);
            return new Result<>("注册成功", 200, "ok");
        }
    }
}
